﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Purchases;

namespace NotebookS2
{
    public partial class CashDisbursement : RibbonForm
    {
        public CashDisbursement()
        {
            InitializeComponent();
        }

        void RemoveDeductionLastRowFirst()
        {
            try
            {
                gridViewDeductions.MoveLast();
                string sItemCode = "";


                sItemCode = gridViewDeductions.GetRowCellValue(gridViewDeductions.FocusedRowHandle, gridViewDeductions.Columns["AcctCode"]).ToString();

                if (sItemCode.Trim() == "")
                {
                    gridViewDeductions.DeleteRow(gridViewDeductions.FocusedRowHandle);
                }
            }
            catch
            {
            }

        }
        void RemoveVouchersLastRowFirst()
        {
            try
            {

                grdViewAPVoucher.MoveLast();


                string sItemCode = "";


                sItemCode = grdViewAPVoucher.GetRowCellValue(grdViewAPVoucher.FocusedRowHandle, grdViewAPVoucher.Columns["VoucherNum"]).ToString();

                if (sItemCode.Trim() == "")
                {
                    grdViewAPVoucher.DeleteRow(grdViewAPVoucher.FocusedRowHandle);
                }
            }
            catch
            {
            }

        }
       
        void ComputeTotal()
        {
            double _TotalInvoice = 0;
            double _TotalReturns = 0;
            double _TotalDeductions = 0;

            txtTotalApplied.Text = "0.00";
            foreach (DataRow oRow in this.VoucherLineItems.Rows)
            {
                if (oRow["AppliedAmount"] != DBNull.Value)
                {
                    _TotalInvoice = _TotalInvoice + Convert.ToDouble(oRow["AppliedAmount"]);
                }
            }

            foreach (DataRow oRow in this.DeductionLineItems.Rows)
            {

                if (oRow["Amount"] != DBNull.Value)
                {
                    _TotalDeductions = _TotalDeductions + Convert.ToDouble(oRow["Amount"]);
                }
            }

            foreach (DataRow oRow in this.PurchaseReturnLineItems.Rows)
            {

                if (oRow["AppliedAmount"] != DBNull.Value)
                {
                    _TotalReturns = _TotalReturns + Convert.ToDouble(oRow["AppliedAmount"]);
                }
            }
            txtTotalApplied.Text = (_TotalInvoice - (_TotalDeductions + _TotalReturns)).ToString();


           

        }

        DataTable InitializeAppliedVoucher()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("DocType", typeof(string));
            dt.Columns.Add("VoucherNum", typeof(string));
            dt.Columns.Add("Amount", typeof(double));
            dt.Columns.Add("AppliedAmount", typeof(double));

            return dt;
        }

        DataTable InitializeCheckLineItems()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("CheckAcctCode", typeof(string));
            dt.Columns.Add("CheckAcctName", typeof(string));
            dt.Columns.Add("BankName", typeof(string));
            dt.Columns.Add("CheckNumber", typeof(string));
            dt.Columns.Add("CheckAmount", typeof(double));
            dt.Columns.Add("CheckDate", typeof(DateTime));
            dt.Columns.Add("CheckStatus", typeof(string));

            return dt;
        }

        DataTable InitializeAppliedReturns()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("ReturnNo", typeof(string));
            dt.Columns.Add("Amount", typeof(double));
            dt.Columns.Add("AppliedAmount", typeof(double));

            return dt;
        }

        DataTable InitializeDeductions()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Particulars", typeof(string));
            dt.Columns.Add("AcctCode", typeof(string));
            dt.Columns.Add("AcctName", typeof(string));
            dt.Columns.Add("Amount", typeof(double));

            return dt;
        }

        public const int ObjectType = 16;
        public string ActiveUserID { get; set; }
        public string ConnectionString { get; set; }

        public string CashGLAcctCode { get; set; }
        public string CashGLAcctName { get; set; }
        public double CashAmount { get; set; }

        public string CheckGLAcctCode { get; set; }
        public string CheckGLAcctName { get; set; }
        public double CheckAmount { get; set; }

        public string FundTransferAcctCode { get; set; }
        public string FundTransferAcctName { get; set; }
        public string FundTransferBank { get; set; }
        public string FundTransferReference { get; set; }
        public DateTime FundTransferDate { get; set; }
        public double FundTransferAmount { get; set; }

        public DataTable VoucherLineItems { get; set; }
        public DataTable DeductionLineItems { get; set; }
        public DataTable PurchaseReturnLineItems { get; set; }
        public DataTable CheckLineItems { get; set; }

        void PostDocument(string _DocNumber)
        {
            CashDisbursementManager oManager = new CashDisbursementManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.PostDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Posted";


            foreach (DataRow oRow in VoucherLineItems.Rows)
            {
                if (oRow["DocType"].ToString() == "A/P Voucher")
                {
                    APVoucherManager oAPVManager = new APVoucherManager();

                    oAPVManager.ConnectionString = this.ConnectionString;
                    oAPVManager.Open();

                    oAPVManager.CloseDocument(oRow["VoucherNum"].ToString());

                    oAPVManager.Close();

                }
                else if (oRow["DocType"].ToString() == "Receiving Report")
                {
                    GoodsReceivingReportManager oGRPOManager = new GoodsReceivingReportManager();

                    oGRPOManager.ConnectionString = this.ConnectionString;
                    oGRPOManager.Open();

                    oGRPOManager.SetAppliedAmount(oRow["DocNum"].ToString(), oRow["AppliedAmount"].ToString());

                    oGRPOManager.Close();
                }
            }

        }

        void CancelDocument(string _DocNumber)
        {
            CashDisbursementManager oManager = new CashDisbursementManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CancelDocument(_DocNumber);

            oManager.Close();
            txtStatus.Text = "Canceled";
        }

        void CloseDocument(string _DocNumber)
        {
            CashDisbursementManager oManager = new CashDisbursementManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            oManager.CloseDocument(_DocNumber);

            oManager.Close();

            txtStatus.Text = "Closed";
        }

        void InitializeSeries()
        {
            string ObjectType = "17";
            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();


            if (oSeries.IsAutoNumber(ObjectType) == true)
            {
                txtDocNum.Properties.ReadOnly = true;
                txtDocNum.Text = "##########";

            }
            oSeries.Close();
        }

        string GetNewSeries()
        {
            string _SERIES = "";
            string _OBJECT_TYPE = "17";

            SeriesManager oSeries = new SeriesManager();
            oSeries.ConnectionString = this.ConnectionString;
            oSeries.Open();
            if (oSeries.IsAutoNumber(_OBJECT_TYPE) == true)
            {
                _SERIES = oSeries.GetNewSeries(_OBJECT_TYPE);
            }
            else
            {
                _SERIES = txtDocNum.Text;
            }
            oSeries.Close();

            return _SERIES;
        }

        void DisableControls()
        {
            
            btnAdd.Enabled = false;
            radioSelection.Enabled = false;
            txtReference.Enabled = false;
            txtDocNum.Enabled = false;

            txtRemarks.Enabled = false;
            txtStatus.Enabled = false;
            dtDateCreated.Enabled = false;
            txtCreatedBy.Enabled = false;
            dtDateModified.Enabled = false;
            txtModifiedBy.Enabled = false;
            
            txtReceiptNumber.Enabled = false;
            dtPostingDate.Enabled = false;
            txtTINNumber.Enabled = false;
            txtCustCode.Enabled = false;
            txtCustName.Enabled = false;
            txtAddress.Enabled = false;
            //txtCashAmount.Enabled = false;


        }

        void EnableControls()
        {
            
            btnAdd.Enabled = true;
            txtDocNum.Enabled = true;
            radioSelection.Enabled = true;
            txtReference.Enabled = true;
            txtRemarks.Enabled = true;
            txtStatus.Enabled = true;
            dtDateCreated.Enabled = true;
            txtCreatedBy.Enabled = true;
            dtDateModified.Enabled = true;
            txtModifiedBy.Enabled = true;
            
            txtReceiptNumber.Enabled = true;
            dtPostingDate.Enabled = true;
            txtTINNumber.Enabled = true;
            txtCustCode.Enabled = true;
            txtCustName.Enabled = true;
            txtAddress.Enabled = true;
            //txtCashAmount.Enabled = true;

        }

        public void ShowSearch()
        {
            FND_CASHDISBURSEMENT oForm = new FND_CASHDISBURSEMENT();
         
            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            CashDisbursementManager oManager = new CashDisbursementManager();
            DataTable dt = new DataTable();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dt = oManager.GetDisbursement(oForm.SelectedDocumentNumber);

            oManager.Close();

            DataRow oRow = dt.Rows[0];

            txtDocNum.Text = oRow["DocNum"].ToString();
            txtReference.Text = oRow["Reference"].ToString();
            txtRemarks.Text = oRow["Remarks"].ToString();
            
            txtReceiptNumber.Text = oRow["ReceiptNumber"].ToString();
            dtPostingDate.EditValue = (DateTime)oRow["PostingDate"];
            txtTINNumber.Text = oRow["TINNumber"].ToString();

            radioSelection.SelectedIndex = Convert.ToInt16(Convert.ToBoolean(oRow["IsAccount"]));
            txtCustCode.Text = oRow["PayeeCode"].ToString();
            txtCustName.Text = oRow["PayeeName"].ToString();
            txtAddress.Text = oRow["AddressOrRemarks"].ToString();

            #region Cash Entries
            this.CashGLAcctCode = oRow["CashAcctCode"].ToString();
            this.CashGLAcctName = oRow["CashAcctName"].ToString();
            this.CashAmount = Convert.ToDouble(oRow["CashAmount"]);
            #endregion

            #region Check Entries
            this.CheckGLAcctCode = oRow["CheckAcctCode"].ToString();
            this.CheckGLAcctName = oRow["CheckAcctName"].ToString();
            this.CheckAmount = Convert.ToDouble(oRow["CheckAmount"]);
            #endregion

            #region Bank Transfer
            this.FundTransferAcctCode = oRow["FundTrsfAcctCode"].ToString();
            this.FundTransferAcctName = oRow["FundTrsfrAcctName"].ToString();
            this.FundTransferBank = oRow["FundTrsfrBank"].ToString();
            this.FundTransferReference = oRow["FundTrsfrReference"].ToString();
            this.FundTransferDate = Convert.ToDateTime(oRow["FundTrsfrDate"]);
            this.FundTransferAmount = Convert.ToDouble(oRow["FundTrsfrAmount"]);
            #endregion

            txtTotal.Text = oRow["Total"].ToString();
            //txtCashAmount.Text = oRow["CashAmount"].ToString();

            txtStatus.Text = oRow["DocStatus"].ToString();
            txtCreatedBy.Text = oRow["CreatedBy"].ToString();
            dtDateCreated.EditValue = (DateTime)oRow["DateCreated"];
            txtModifiedBy.Text = oRow["ModifiedBy"].ToString();
            dtDateModified.EditValue = (DateTime)oRow["DateModified"];

     

            this.PurchaseReturnLineItems = oManager.GetAppliedPurchaseReturn(txtDocNum.Text);
            this.VoucherLineItems = oManager.GetAppliedVoucher(txtDocNum.Text);
            this.DeductionLineItems = oManager.GetAppliedDeductions(txtDocNum.Text);

            gridControlDeductions.DataSource = this.DeductionLineItems;
            gridControlDeductions.Refresh();

            gridControlAPVoucher.DataSource = this.VoucherLineItems;
            gridControlAPVoucher.Refresh();

            ComputeTotal();
            if (txtStatus.Text == "Draft")
            {
                if (gridViewDeductions.RowCount <= 0)
                {
                    gridViewDeductions.AddNewRow();
                }

                if (grdViewAPVoucher.RowCount <= 0)
                {
                    grdViewAPVoucher.AddNewRow();
                }

                
            }
            btnAdd.Text = "Update";

        }


        private void CashDisbursement_Load(object sender, EventArgs e)
        {
            int iZeroAmount = 0;

            this.VoucherLineItems = InitializeAppliedVoucher();
            this.PurchaseReturnLineItems = InitializeAppliedReturns();
            this.DeductionLineItems = InitializeDeductions();
            this.CheckLineItems = InitializeCheckLineItems();

            gridControlDeductions.DataSource = this.DeductionLineItems;
            gridControlDeductions.Refresh();

            gridControlAPVoucher.DataSource = this.VoucherLineItems;
            gridControlAPVoucher.Refresh();

            if (gridViewDeductions.RowCount <= 0)
            {
                gridViewDeductions.AddNewRow();
            }

            if (grdViewAPVoucher.RowCount <= 0)
            {
                grdViewAPVoucher.AddNewRow();
            }

            

            #region Cash Entries
            this.CashGLAcctCode = "";
            this.CashGLAcctName = "";

            this.CashAmount = iZeroAmount;
            #endregion

            #region Check Entries
            this.CheckGLAcctCode = "";
            this.CheckGLAcctName = "";
            this.CheckAmount = iZeroAmount;
            #endregion

            #region Bank Transfer
            this.FundTransferAcctCode = "";
            this.FundTransferAcctName = "";
            this.FundTransferBank = "";
            this.FundTransferReference = "";
            this.FundTransferDate = System.DateTime.Now;
            this.FundTransferAmount = iZeroAmount;
            #endregion
            
            
            

            InitializeSeries();

            txtCreatedBy.Text = this.ActiveUserID;
            txtModifiedBy.Text = this.ActiveUserID;

            dtDateCreated.EditValue = System.DateTime.Now;
            dtDateModified.EditValue = System.DateTime.Now;
            dtPostingDate.EditValue = System.DateTime.Now;


        }

        private void btnPaymentMeans_Click(object sender, EventArgs e)
        {
            PaymentMeans oForm = new PaymentMeans();

            oForm.ConnectionString = this.ConnectionString;
            oForm.DocumentCode = "Check Voucher";
            oForm.CheckLineItems = this.CheckLineItems;

            if (txtStatus.Text != "Draft")
            {
                oForm.IsReadOnly = true;
            }

            #region Cash Entries
            oForm.CashGLAcctCode = this.CashGLAcctCode;
            oForm.CashGLAcctName = this.CashGLAcctName;
            oForm.CashAmount = Convert.ToDouble(this.CashAmount);
            #endregion

            #region Check Entries
            oForm.CheckGLAcctCode = this.CheckGLAcctCode.ToString();
            oForm.CheckGLAcctName = this.CheckGLAcctName.ToString();
            oForm.CheckAmount = Convert.ToDouble(this.CheckAmount);
            
            #endregion

            #region Bank Transfer
            oForm.FundTransferAcctCode = this.FundTransferAcctCode;
            oForm.FundTransferAcctName = this.FundTransferAcctName;
            oForm.FundTransferBank = this.FundTransferBank;
            oForm.FundTransferReference = this.FundTransferReference;
            oForm.FundTransferDate = this.FundTransferDate;
            oForm.FundTransferAmount = this.FundTransferAmount;
            #endregion
            


            oForm.ShowDialog();

            if (oForm.IsCancelled == false)
            {
                #region Cash Entries
                this.CashGLAcctCode = oForm.CashGLAcctCode;
                this.CashGLAcctName = oForm.CashGLAcctName;
                this.CashAmount = Convert.ToDouble(oForm.CashAmount);
                #endregion

                #region Check Entries
                this.CheckGLAcctCode = oForm.CheckGLAcctCode.ToString();
                this.CheckGLAcctName = oForm.CheckGLAcctName.ToString();
                this.CheckAmount = Convert.ToDouble(oForm.CheckAmount);
                #endregion

                #region Bank Transfer
                this.FundTransferAcctCode = oForm.FundTransferAcctCode;
                this.FundTransferAcctName = oForm.FundTransferAcctName;
                this.FundTransferBank = oForm.FundTransferBank;
                this.FundTransferReference = oForm.FundTransferReference;
                this.FundTransferDate = oForm.FundTransferDate;
                this.FundTransferAmount = oForm.FundTransferAmount;
                #endregion

                txtTotal.Text = (this.CashAmount + this.CheckAmount + this.FundTransferAmount).ToString();
            }
        }

        private void xtraScrollableControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            RemoveDeductionLastRowFirst();
            RemoveVouchersLastRowFirst();
            
            if (btnAdd.Text == "Add")
            {
                if (txtCustCode.Text != "")
                {
                    CashDisbursementManager oManager = new CashDisbursementManager();
                    DisbursementUnit oUnit = new DisbursementUnit();

                    txtDocNum.Text = GetNewSeries();

                    oUnit.DocumentNumber = txtDocNum.Text;
                    oUnit.Reference = txtReference.Text;
                    oUnit.Remarks = txtRemarks.Text;
                    
                    oUnit.ReceiptNumber = txtReceiptNumber.Text;

                    oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                    oUnit.TINNumber = txtTINNumber.Text;

                    if (radioSelection.SelectedIndex == 0)
                    {
                        oUnit.IsAccount = false;
                    }
                    else
                    {
                        oUnit.IsAccount = true;
                    }

                    oUnit.CardCode = txtCustCode.Text;
                    oUnit.CardName = txtCustName.Text;
                    oUnit.Address = txtAddress.Text;

                    #region Cash Entries
                    oUnit.CashGLAcctCode = this.CashGLAcctCode;
                    oUnit.CashGLAcctName = this.CashGLAcctName;
                    oUnit.CashAmount = Convert.ToDouble(this.CashAmount);
                    #endregion

                    #region Check Entries
                    oUnit.CheckGLAcctCode = this.CheckGLAcctCode.ToString();
                    oUnit.CheckGLAcctName = this.CheckGLAcctName.ToString();
                    oUnit.CheckAmount = Convert.ToDouble(this.CheckAmount);
                    #endregion

                    #region Bank Transfer
                    oUnit.FundTransferAcctCode = this.FundTransferAcctCode;
                    oUnit.FundTransferAcctName = this.FundTransferAcctName;
                    oUnit.FundTransferBank = this.FundTransferBank;
                    oUnit.FundTransferReference = this.FundTransferReference;
                    oUnit.FundTransferDate = this.FundTransferDate;
                    oUnit.FundTransferAmount = this.FundTransferAmount;
                    #endregion

                    oUnit.TotalAmount = Convert.ToDouble(txtTotal.Text);
                    oUnit.DocumentStatus = txtStatus.Text;
                    oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                    oUnit.CreatedBy = txtCreatedBy.Text;
                    oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                    oUnit.ModifiedBy = txtModifiedBy.Text;

                    oUnit.VoucherLineItems = this.VoucherLineItems;
                    oUnit.PurchaseReturnLineItems = this.PurchaseReturnLineItems;
                    oUnit.DeductionLineItems = this.DeductionLineItems;
                    oUnit.CheckLineItems = this.CheckLineItems;

                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    oManager.AddNewDisbursement(oUnit);

                    oManager.Close();

                    btnAdd.Text = "Update";

                    MessageBox.Show("Successfully added!!!", "Cash Receipt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Please select Payee!!!", "Cash Receipt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                CashDisbursementManager oManager = new CashDisbursementManager();
                DisbursementUnit oUnit = new DisbursementUnit();

                oUnit.DocumentNumber = txtDocNum.Text;
                oUnit.Reference = txtReference.Text;
                oUnit.Remarks = txtRemarks.Text;
                
                oUnit.ReceiptNumber = txtReceiptNumber.Text;

                oUnit.PostingDate = (DateTime)dtPostingDate.EditValue;
                oUnit.TINNumber = txtTINNumber.Text;

                if (radioSelection.SelectedIndex == 0)
                {
                    oUnit.IsAccount = false;
                }
                else
                {
                    oUnit.IsAccount = true;
                }

                oUnit.CardCode = txtCustCode.Text;
                oUnit.CardName = txtCustName.Text;
                oUnit.Address = txtAddress.Text;


                #region Cash Entries
                oUnit.CashGLAcctCode = this.CashGLAcctCode;
                oUnit.CashGLAcctName = this.CashGLAcctName;
                oUnit.CashAmount = Convert.ToDouble(this.CashAmount);
                #endregion

                #region Check Entries
                oUnit.CheckGLAcctCode = this.CheckGLAcctCode.ToString();
                oUnit.CheckGLAcctName = this.CheckGLAcctName.ToString();
                oUnit.CheckAmount = Convert.ToDouble(this.CheckAmount);
                #endregion

                #region Bank Transfer
                oUnit.FundTransferAcctCode = this.FundTransferAcctCode;
                oUnit.FundTransferAcctName = this.FundTransferAcctName;
                oUnit.FundTransferBank = this.FundTransferBank;
                oUnit.FundTransferReference = this.FundTransferReference;
                oUnit.FundTransferDate = this.FundTransferDate;
                oUnit.FundTransferAmount = this.FundTransferAmount;
                #endregion
                oUnit.TotalAmount = Convert.ToDouble(txtTotal.Text);
                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = (DateTime)dtDateCreated.EditValue;
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = (DateTime)dtDateModified.EditValue;
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oUnit.VoucherLineItems = this.VoucherLineItems;
                oUnit.PurchaseReturnLineItems = this.PurchaseReturnLineItems;
                oUnit.DeductionLineItems = this.DeductionLineItems;
                oUnit.CheckLineItems = this.CheckLineItems;

                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oManager.UpdateDisbursement(oUnit);

                oManager.Close();

                MessageBox.Show("Successfully updated!!!", "Cash Receipt", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txtCustCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (radioSelection.SelectedIndex == 0)
            {
                FND_BP _SearchBP = new FND_BP();

                AddressBookManager oBPManager = new AddressBookManager();
                TaxManager oTxManager = new TaxManager();

                try
                {


                    _SearchBP.ConnectionString = this.ConnectionString;
                    _SearchBP.BusinessPartnerType = "Vendor";
                    _SearchBP.ShowDialog();

                    txtCustCode.Text = _SearchBP.CardCode;
                    txtCustName.Text = _SearchBP.CardName;
                    txtTINNumber.Text = _SearchBP.TIN;
                    txtAddress.Text = _SearchBP.Address;
                    txtAddress.Enabled = false;
                }
                catch
                {
                }
            }
            else if (radioSelection.SelectedIndex == 1)
            {
                FND_BP _SearchBP = new FND_BP();

                AddressBookManager oBPManager = new AddressBookManager();
                TaxManager oTxManager = new TaxManager();

                try
                {


                    _SearchBP.ConnectionString = this.ConnectionString;
                    _SearchBP.BusinessPartnerType = "Customer";
                    _SearchBP.ShowDialog();

                    txtCustCode.Text = _SearchBP.CardCode;
                    txtCustName.Text = _SearchBP.CardName;
                    txtTINNumber.Text = _SearchBP.TIN;
                    txtAddress.Text = _SearchBP.Address;
                    txtAddress.Enabled = false;
                }
                catch
                {
                }
            }

            else
            {
                FND_GLACCOUNT oForm = new FND_GLACCOUNT();

                oForm.ConnectionString = this.ConnectionString;
                oForm.ShowDialog();

                txtTINNumber.Text = "N/A";
                txtAddress.Enabled = true;
                txtCustCode.Text = oForm.SelectedGLAcctCode;
                txtCustName.Text = oForm.SelectedGLAcctName;

            }
        }

        private void txtStatus_EditValueChanged(object sender, EventArgs e)
        {
            if (txtStatus.Text == "Posted")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Draft")
            {
                EnableControls();
                txtStatus.Properties.Buttons[0].Visible = true;
                txtStatus.Properties.Buttons[1].Visible = true;
            }

            if (txtStatus.Text == "Canceled")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }

            if (txtStatus.Text == "Closed")
            {
                DisableControls();
                txtStatus.Properties.Buttons[0].Visible = false;
                txtStatus.Properties.Buttons[1].Visible = false;
            }
        }

        private void radioSelection_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioSelection.SelectedIndex == 0)
            {
                
                lblAddress.Text = "Address:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";


            }
            else if (radioSelection.SelectedIndex == 1)
            {
                
                lblAddress.Text = "Address:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";


            }
            else
            {
                
                lblAddress.Text = "Remarks:";

                txtCustCode.Text = "";
                txtCustName.Text = "";
                txtAddress.Text = "";
                txtTINNumber.Text = "";
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtStatus_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {

            switch (e.Button.Index)
            {
                case 0:
                    // Post

                    if (MessageBox.Show("Are you sure you want to post this Document?", "Post Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        PostDocument(txtDocNum.Text);
                    }

                    break;
                case 1:
                    // Cancel:
                    if (txtStatus.Text == "Draft")
                    {
                        if (MessageBox.Show("Are you sure you want to cancel this Document?", "Cancel Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            CancelDocument(txtDocNum.Text);
                        }
                    }
                    else if (txtStatus.Text == "Posted")
                    {
                        if (MessageBox.Show("Are you sure you want to close this Document?", "Close Document", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                        {
                            CloseDocument(txtDocNum.Text);
                        }
                    }
                    break;
            }
        }

       

        private void txtVoucherNo_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            int iFocusRowHandle = grdViewAPVoucher.FocusedRowHandle;
            string sDocumentType = grdViewAPVoucher.GetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["DocType"]).ToString();

            if (sDocumentType != null)
            {
                if (sDocumentType == "A/P Voucher")
                {
                    FND_CASHDISBURSE_AP_VOUCHER oForm = new FND_CASHDISBURSE_AP_VOUCHER();
                    oForm.ConnectionString = this.ConnectionString;
                    oForm.ShowDialog();

                    string sAPVIDocNumber = oForm.SelectedDocumentNumber;

                    if (sAPVIDocNumber != "")
                    {
                        APVoucherManager oAPVoucherManager = new APVoucherManager();

                        oAPVoucherManager.ConnectionString = this.ConnectionString;
                        oAPVoucherManager.Open();

                        DataTable dt = oAPVoucherManager.GetAPVoucher(sAPVIDocNumber);

                        DataRow oRow = dt.Rows[0];

                        oAPVoucherManager.Close();

                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["VoucherNum"], sAPVIDocNumber);
                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["Amount"], oRow["TotalAmount"].ToString());
                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["AppliedAmount"], oRow["TotalAmount"].ToString());
                    }


                }
                else if (sDocumentType == "Receiving Report")
                {
                    FND_CASHDISBURSE_GRPO oForm = new FND_CASHDISBURSE_GRPO();
                    oForm.ConnectionString = this.ConnectionString;
                    oForm.VendorCode = txtCustCode.Text;
                    oForm.ShowDialog();



                    string sDocNumber = oForm.DocumentNumber;

                    if (sDocNumber != "")
                    {
                        GoodsReceivingReportManager oGRPOManager = new GoodsReceivingReportManager();

                        oGRPOManager.ConnectionString = this.ConnectionString;
                        oGRPOManager.Open();

                        DataRow oRow = oGRPOManager.GetGRPO(sDocNumber);


                        oGRPOManager.Close();

                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["VoucherNum"], sDocNumber);
                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["Amount"], oRow["NetAmount"].ToString());
                        grdViewAPVoucher.SetRowCellValue(iFocusRowHandle, grdViewAPVoucher.Columns["AppliedAmount"], oRow["NetAmount"].ToString());
                    }
                }
            }

        }

      
        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Are you sure you want to delete this row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    gridViewDeductions.DeleteRow(gridViewDeductions.FocusedRowHandle);
                    ComputeTotal();
                }
            }

            if (e.KeyCode == Keys.Down)
            {
                if (gridViewDeductions.IsLastRow)
                {

                    string sItemCode = "";

                    int iCount = gridViewDeductions.FocusedRowHandle;
                    sItemCode = gridViewDeductions.GetRowCellValue(iCount, gridViewDeductions.Columns["AcctCode"]).ToString();

                    if (sItemCode != "" && (txtStatus.Text == "Draft"))
                    {
                        gridViewDeductions.AddNewRow();

                        gridControlDeductions.Select();
                        gridViewDeductions.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                        gridViewDeductions.FocusedColumn = gridViewDeductions.Columns["AcctCode"];
                        gridViewDeductions.ShowEditor();


                    }
                    ComputeTotal();
                }
            }
        }

        private void cboDocTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            int iFocusedRowHandle = grdViewAPVoucher.FocusedRowHandle;
            DevExpress.XtraEditors.ComboBoxEdit oCombo = (DevExpress.XtraEditors.ComboBoxEdit)sender;
            
            grdViewAPVoucher.SetRowCellValue(iFocusedRowHandle, grdViewAPVoucher.Columns["DocType"], oCombo.Text.ToString());

           
        }

        private void gridView2_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Are you sure you want to delete this row?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    grdViewAPVoucher.DeleteRow(grdViewAPVoucher.FocusedRowHandle);
                    ComputeTotal();
                }
            }

            if (e.KeyCode == Keys.Down)
            {
                if (grdViewAPVoucher.IsLastRow)
                {

                    string _VOUCHER_NUMBER = "";

                    int iCount = grdViewAPVoucher.FocusedRowHandle;
                    _VOUCHER_NUMBER = grdViewAPVoucher.GetRowCellValue(iCount, grdViewAPVoucher.Columns["VoucherNum"]).ToString();

                    if (_VOUCHER_NUMBER != "" && (txtStatus.Text == "Draft"))
                    {
                        grdViewAPVoucher.AddNewRow();

                        gridControlAPVoucher.Select();
                        grdViewAPVoucher.FocusedRowHandle = DevExpress.XtraGrid.GridControl.NewItemRowHandle;
                        grdViewAPVoucher.FocusedColumn = grdViewAPVoucher.Columns["VoucherNum"];
                        grdViewAPVoucher.ShowEditor();


                    }
                    ComputeTotal();
                }
            }
        }

        private void txtAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            int iFocusHandle = gridViewDeductions.FocusedRowHandle;
            gridViewDeductions.SetRowCellValue(iFocusHandle, gridViewDeductions.Columns["AcctCode"], oForm.SelectedGLAcctCode);
            gridViewDeductions.SetRowCellValue(iFocusHandle, gridViewDeductions.Columns["AcctName"], oForm.SelectedGLAcctName);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

            PaymentMeans oForm = new PaymentMeans();

            oForm.ConnectionString = this.ConnectionString;
            oForm.DocumentCode = "Fund Transfer";

            if (txtStatus.Text != "Draft")
            {
                oForm.IsReadOnly = true;
            }

            #region Cash Entries
            oForm.CashGLAcctCode = this.CashGLAcctCode;
            oForm.CashGLAcctName = this.CashGLAcctName;
            oForm.CashAmount = Convert.ToDouble(this.CashAmount);
            #endregion

            #region Check Entries
            oForm.CheckGLAcctCode = this.CheckGLAcctCode.ToString();
            oForm.CheckGLAcctName = this.CheckGLAcctName.ToString();
            oForm.CheckAmount = Convert.ToDouble(this.CheckAmount);
            #endregion

            #region Bank Transfer
            oForm.FundTransferAcctCode = this.FundTransferAcctCode;
            oForm.FundTransferAcctName = this.FundTransferAcctName;
            oForm.FundTransferBank = this.FundTransferBank;
            oForm.FundTransferReference = this.FundTransferReference;
            oForm.FundTransferDate = this.FundTransferDate;
            oForm.FundTransferAmount = this.FundTransferAmount;
            #endregion



            oForm.ShowDialog();

            if (oForm.IsCancelled == false)
            {
                #region Cash Entries
                this.CashGLAcctCode = oForm.CashGLAcctCode;
                this.CashGLAcctName = oForm.CashGLAcctName;
                this.CashAmount = Convert.ToDouble(oForm.CashAmount);
                #endregion

                #region Check Entries
                this.CheckGLAcctCode = oForm.CheckGLAcctCode.ToString();
                this.CheckGLAcctName = oForm.CheckGLAcctName.ToString();
                this.CheckAmount = Convert.ToDouble(oForm.CheckAmount);
                #endregion

                #region Bank Transfer
                this.FundTransferAcctCode = oForm.FundTransferAcctCode;
                this.FundTransferAcctName = oForm.FundTransferAcctName;
                this.FundTransferBank = oForm.FundTransferBank;
                this.FundTransferReference = oForm.FundTransferReference;
                this.FundTransferDate = oForm.FundTransferDate;
                this.FundTransferAmount = oForm.FundTransferAmount;
                #endregion

                txtTotal.Text = (this.CashAmount + this.CheckAmount + this.FundTransferAmount).ToString();
            }
        }

        private void xtraScrollableControl2_Click(object sender, EventArgs e)
        {

        }

        private void grdViewAPVoucher_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            ComputeTotal();
        }
    }
}
